package com.zj.fx.service.month;

import cn.hutool.core.util.StrUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zj.fx.common.base.BaseService;
import com.zj.fx.common.support.ConvertUtil;
import com.zj.fx.mapper.info.InfoWeightMapper;
import com.zj.fx.mapper.month.MonthResultMapper;
import com.zj.fx.model.custom.Tablepar;
import com.zj.fx.model.info.InfoSupply;
import com.zj.fx.model.info.InfoWeight;
import com.zj.fx.model.info.InfoWeightExample;
import com.zj.fx.model.month.*;
import com.zj.fx.shiro.util.ShiroUtils;
import com.zj.fx.util.SnowflakeIdWorker;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;

/**
 * 月评价结果 MonthResultService
 *
 * @author SQL_自动生成
 * @Title: MonthResultService.java 
 * @Package com.zj.fx.service 
 * @email ${email}
 * @date 2023-06-20 17:32:45  
 **/
@Service
public class MonthResultService implements BaseService<MonthResult, MonthResultExample> {
    @Autowired
    private MonthResultMapper monthResultMapper;

    @Autowired
    private InfoWeightMapper infoWeightMapper;


    /**
     * 分页查询
     *
     * @param
     * @param
     * @return
     */
    public PageInfo<MonthResult> list(Tablepar tablepar, MonthResult monthResult) {
        MonthResultExample testExample = new MonthResultExample();
        //搜索
        if (StrUtil.isNotEmpty(tablepar.getSearchText())) {//小窗体
            testExample.createCriteria().andLikeQuery2(tablepar.getSearchText());
        } else {//大搜索
            testExample.createCriteria().andLikeQuery(monthResult);
        }
        //表格排序
        //if(StrUtil.isNotEmpty(tablepar.getOrderByColumn())) {
        //	testExample.setOrderByClause(StringUtils.toUnderScoreCase(tablepar.getOrderByColumn()) +" "+tablepar.getIsAsc());
        //}else{
        //	testExample.setOrderByClause("id ASC");
        //}
        PageHelper.startPage(tablepar.getPage(), tablepar.getLimit());
        List<MonthResult> list = monthResultMapper.selectByExample(testExample);
        PageInfo<MonthResult> pageInfo = new PageInfo<MonthResult>(list);
        return pageInfo;
    }

    @Override
    public int deleteByPrimaryKey(String ids) {

        List<String> lista = ConvertUtil.toListStrArray(ids);
        MonthResultExample example = new MonthResultExample();
        example.createCriteria().andIdIn(lista);
        return monthResultMapper.deleteByExample(example);


    }


    @Override
    public MonthResult selectByPrimaryKey(String id) {

        return monthResultMapper.selectByPrimaryKey(id);

    }


    @Override
    public int updateByPrimaryKeySelective(MonthResult record) {
        return monthResultMapper.updateByPrimaryKeySelective(record);
    }


    /**
     * 添加
     */
    @Override
    public int insertSelective(MonthResult record) {

        //添加雪花主键id
        record.setId(SnowflakeIdWorker.getUUID());

        return monthResultMapper.insertSelective(record);
    }


    @Override
    public int updateByExampleSelective(MonthResult record, MonthResultExample example) {

        return monthResultMapper.updateByExampleSelective(record, example);
    }


    @Override
    public int updateByExample(MonthResult record, MonthResultExample example) {

        return monthResultMapper.updateByExample(record, example);
    }

    @Override
    public List<MonthResult> selectByExample(MonthResultExample example) {

        return monthResultMapper.selectByExample(example);
    }


    @Override
    public long countByExample(MonthResultExample example) {

        return monthResultMapper.countByExample(example);
    }


    @Override
    public int deleteByExample(MonthResultExample example) {

        return monthResultMapper.deleteByExample(example);
    }

    public List<MonthResult> findExistbyTaskId(String taskId) {
        MonthResultExample monthResultExample = new MonthResultExample();
        MonthResultExample.Criteria criteria = monthResultExample.createCriteria();
        criteria.andFkTaskIdEqualTo(taskId);
        return monthResultMapper.selectByExample(monthResultExample);
    }

    private String insertSelectiveReturnId(MonthResult record) {

        //添加雪花主键id
        record.setId(SnowflakeIdWorker.getUUID());

        monthResultMapper.insertSelective(record);

        return record.getId();
    }

    public String createResult(MonthSupplytask monthSupplytask, String id, InfoSupply infoSupply,String resultState,String nessessaryState,String fullMark) {
        MonthResult monthResult = new MonthResult();
        monthResult.setId(SnowflakeIdWorker.getUUID());
        monthResult.setOrgnization(infoSupply.getOrganization());
        monthResult.setOrgnizationName(infoSupply.getOrganizationName());
        monthResult.setSupplyId(infoSupply.getId());
        monthResult.setSupplyName(infoSupply.getName());
        monthResult.setTaskMonth(monthSupplytask.getTaskMonth());
        monthResult.setFkTaskId(id);
        monthResult.setStar(infoSupply.getStarrating());
        monthResult.setIsNeccessary(nessessaryState);
        monthResult.setIsIssue(resultState);
        monthResult.setFullMark(fullMark);
        return this.insertSelectiveReturnId(monthResult);
    }

    /**
     * 分页查询
     *
     * @param
     * @param
     * @return
     */
    public List<MonthResult> list4Score(Tablepar tablepar,MonthResult monthResult) {
        MonthResultExample testExample = new MonthResultExample();
        //搜索
        if (StrUtil.isNotEmpty(tablepar.getSearchText())) {//小窗体
            testExample.createCriteria().andLikeQuery2(tablepar.getSearchText());
        } else {//大搜索
            testExample.createCriteria().andLikeQuery(monthResult);
        }
        List<MonthResult> results = monthResultMapper.selectByExample(testExample);
        //查询权重表对应各项目权重
        InfoWeightExample infoWeightExample = new InfoWeightExample();
        InfoWeightExample.Criteria criteria = infoWeightExample.createCriteria();
        criteria.andWeightIsNotNull();
        List<InfoWeight> infoWeights = infoWeightMapper.selectByExample(infoWeightExample);
        BigDecimal type1Weight = BigDecimal.ZERO;
        BigDecimal type2Weight = BigDecimal.ZERO;
        BigDecimal type3Weight = BigDecimal.ZERO;
        BigDecimal type4Weight = BigDecimal.ZERO;
        BigDecimal type5Weight = BigDecimal.ZERO;
        BigDecimal type6Weight = BigDecimal.ZERO;
        BigDecimal type7Weight = BigDecimal.ZERO;
        for (InfoWeight infoWeight : infoWeights) {
            if ("1".equals(infoWeight.getNum())){
                type1Weight = new BigDecimal(infoWeight.getWeight());
            } else if ("2".equals(infoWeight.getNum())){
                type2Weight = new BigDecimal(infoWeight.getWeight());
            } else if ("3".equals(infoWeight.getNum())){
                type3Weight = new BigDecimal(infoWeight.getWeight());
            } else if ("4".equals(infoWeight.getNum())){
                type4Weight = new BigDecimal(infoWeight.getWeight());
            } else if ("5".equals(infoWeight.getNum())){
                type5Weight = new BigDecimal(infoWeight.getWeight());
            } else if ("6".equals(infoWeight.getNum())){
                type6Weight = new BigDecimal(infoWeight.getWeight());
            } else if ("7".equals(infoWeight.getNum())){
                type7Weight = new BigDecimal(infoWeight.getWeight());
            }
        }
        for (MonthResult result : results) {
            MonthResultDtl monthResultDtl = new MonthResultDtl();
            monthResultDtl.setFkResultId(result.getId());
            List<MonthResultDtl> monthResultDtls = monthResultMapper.list4Score(monthResultDtl);
            BigDecimal totalScore = BigDecimal.ZERO;
            BigDecimal totalMark = BigDecimal.ZERO;
            for (MonthResultDtl resultDtl : monthResultDtls) {
                if ("1".equals(resultDtl.getTypeId())){
                    result.setYingxiaoScore(resultDtl.getScore());
                    totalScore = totalScore.add(new BigDecimal(resultDtl.getScore()).multiply(type1Weight));
                } else if ("2".equals(resultDtl.getTypeId())){
                    result.setYunjianScore(resultDtl.getScore());
                    totalScore = totalScore.add(new BigDecimal(resultDtl.getScore()).multiply(type2Weight));
                } else if ("3".equals(resultDtl.getTypeId())){
                    result.setAnquanScore(resultDtl.getScore());
                    totalScore = totalScore.add(new BigDecimal(resultDtl.getScore()).multiply(type3Weight));
                } else if ("4".equals(resultDtl.getTypeId())){
                    result.setDangjianScore(resultDtl.getScore());
                    totalScore = totalScore.add(new BigDecimal(resultDtl.getScore()).multiply(type4Weight));
                } else if ("5".equals(resultDtl.getTypeId())){
                    result.setShuzihuaScore(resultDtl.getScore());
                    totalScore = totalScore.add(new BigDecimal(resultDtl.getScore()).multiply(type5Weight));
                } else if ("6".equals(resultDtl.getTypeId())){
                    result.setFazhanScore(resultDtl.getScore());
                    totalScore = totalScore.add(new BigDecimal(resultDtl.getScore()).multiply(type6Weight));
                } else if ("7".equals(resultDtl.getTypeId())){
                    result.setWuxiaoScore(resultDtl.getScore());
                    totalScore = totalScore.add(new BigDecimal(resultDtl.getScore()).multiply(type7Weight));
                }
                totalMark = totalMark.add(new BigDecimal(resultDtl.getScore()));
            }
            result.setTotalScore(totalScore.setScale(2));
            result.setTotalMark(totalMark.setScale(2));
        }
        return results;
    }

    /**
     * 分页查询
     *
     * @param
     * @param
     * @return
     */
    public List<MonthResult> list4Score(MonthResult monthResult) {
        MonthResultExample testExample = new MonthResultExample();
        testExample.createCriteria().andTaskMonthEqualTo(monthResult.getTaskMonth());
        List<MonthResult> results = monthResultMapper.selectByExample(testExample);
        //查询权重表对应各项目权重
        InfoWeightExample infoWeightExample = new InfoWeightExample();
        InfoWeightExample.Criteria criteria = infoWeightExample.createCriteria();
        criteria.andWeightIsNotNull();
        List<InfoWeight> infoWeights = infoWeightMapper.selectByExample(infoWeightExample);
        BigDecimal type1Weight = BigDecimal.ZERO;
        BigDecimal type2Weight = BigDecimal.ZERO;
        BigDecimal type3Weight = BigDecimal.ZERO;
        BigDecimal type4Weight = BigDecimal.ZERO;
        BigDecimal type5Weight = BigDecimal.ZERO;
        BigDecimal type6Weight = BigDecimal.ZERO;
        BigDecimal type7Weight = BigDecimal.ZERO;
        for (InfoWeight infoWeight : infoWeights) {
            if ("1".equals(infoWeight.getNum())){
                type1Weight = new BigDecimal(infoWeight.getWeight());
            } else if ("2".equals(infoWeight.getNum())){
                type2Weight = new BigDecimal(infoWeight.getWeight());
            } else if ("3".equals(infoWeight.getNum())){
                type3Weight = new BigDecimal(infoWeight.getWeight());
            } else if ("4".equals(infoWeight.getNum())){
                type4Weight = new BigDecimal(infoWeight.getWeight());
            } else if ("5".equals(infoWeight.getNum())){
                type5Weight = new BigDecimal(infoWeight.getWeight());
            } else if ("6".equals(infoWeight.getNum())){
                type6Weight = new BigDecimal(infoWeight.getWeight());
            } else if ("7".equals(infoWeight.getNum())){
                type7Weight = new BigDecimal(infoWeight.getWeight());
            }
        }
        for (MonthResult result : results) {
            MonthResultDtl monthResultDtl = new MonthResultDtl();
            monthResultDtl.setFkResultId(result.getId());
            List<MonthResultDtl> monthResultDtls = monthResultMapper.list4Score(monthResultDtl);
            BigDecimal totalScore = BigDecimal.ZERO;
            BigDecimal totalMark = BigDecimal.ZERO;
            for (MonthResultDtl resultDtl : monthResultDtls) {
                if ("1".equals(resultDtl.getTypeId())){
                    result.setYingxiaoScore(resultDtl.getScore());
                    totalScore = totalScore.add(new BigDecimal(resultDtl.getScore()).multiply(type1Weight));
                } else if ("2".equals(resultDtl.getTypeId())){
                    result.setYunjianScore(resultDtl.getScore());
                    totalScore = totalScore.add(new BigDecimal(resultDtl.getScore()).multiply(type2Weight));
                } else if ("3".equals(resultDtl.getTypeId())){
                    result.setAnquanScore(resultDtl.getScore());
                    totalScore = totalScore.add(new BigDecimal(resultDtl.getScore()).multiply(type3Weight));
                } else if ("4".equals(resultDtl.getTypeId())){
                    result.setDangjianScore(resultDtl.getScore());
                    totalScore = totalScore.add(new BigDecimal(resultDtl.getScore()).multiply(type4Weight));
                } else if ("5".equals(resultDtl.getTypeId())){
                    result.setShuzihuaScore(resultDtl.getScore());
                    totalScore = totalScore.add(new BigDecimal(resultDtl.getScore()).multiply(type5Weight));
                } else if ("6".equals(resultDtl.getTypeId())){
                    result.setFazhanScore(resultDtl.getScore());
                    totalScore = totalScore.add(new BigDecimal(resultDtl.getScore()).multiply(type6Weight));
                } else if ("7".equals(resultDtl.getTypeId())){
                    result.setWuxiaoScore(resultDtl.getScore());
                    totalScore = totalScore.add(new BigDecimal(resultDtl.getScore()).multiply(type7Weight));
                }
                totalMark = totalMark.add(new BigDecimal(resultDtl.getScore()));
            }
            result.setTotalScore(totalScore.setScale(2));
            result.setTotalMark(totalMark.setScale(2));
        }
        return results;
    }
}
